package com.xgq.drink.controller;

import com.xgq.drink.entity.ChatContact;
import com.xgq.drink.entity.R;
import com.xgq.drink.service.IChatContactService;
import com.xgq.drink.utils.JwtTokenUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/chat/contacts")
@Tag(name = "聊天联系人管理")
public class ChatContactController {
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private IChatContactService contactService;

    @GetMapping
    @Operation(summary = "获取全部联系人")
    public R getContacts(@RequestHeader("Authorization") String token) {
        Long userId = getUserIdFromToken(token); // 从token解析用户ID的实现需补充
        Long brandId = jwtTokenUtil.getStoreIdFromToken(token);
        if (brandId != null) {
            userId = Long.valueOf("1000"+brandId);
        }
        System.out.println("获取全部联系人");
        System.out.println(userId);
        return contactService.getAllContacts(userId);
    }

    @DeleteMapping
    @Operation(summary = "批量删除联系人")
    public R deleteContacts(@RequestHeader("Authorization") String token,
                                   @RequestBody List<Long> targetIds) {
        Long userId = getUserIdFromToken(token);
        return contactService.batchDeleteContacts(userId, targetIds);
    }

    @PostMapping("/clearUnread")
    @Operation(summary = "清空未读数")
    public R clearUnread(@RequestHeader("Authorization") String token,
                                @RequestParam Long targetId) {
        Long userId = getUserIdFromToken(token);
        Long brandId = jwtTokenUtil.getStoreIdFromToken(token);
        if (brandId != null) {
            userId = Long.valueOf("1000"+brandId);
        }
        return contactService.clearUnreadCount(userId, targetId);
    }

    @PostMapping
    @Operation(summary = "创建/更新联系人")
    public R createContact(@RequestHeader("Authorization") String token,
                                        @RequestBody ChatContact contact) {
        Long userId = getUserIdFromToken(token);
        Long brandId = jwtTokenUtil.getStoreIdFromToken(token);
        if (brandId != null) {
            userId = Long.valueOf("1000"+brandId);
        }
        contact.setUserId(userId);
        return contactService.createOrUpdateContact(contact);
    }

    @PutMapping("/{contactId}/time")
    @Operation(summary = "修改创建时间")
    public R updateTime(@PathVariable Long contactId,
                                @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime newTime) {
        return contactService.updateCreateTime(contactId, newTime);
    }

    private Long getUserIdFromToken(String token) {
        return Long.valueOf(jwtTokenUtil.getUserIdFromToken(token)); // 示例返回值
    }
}
